<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1300" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="产品型号" prop="goods_model">
                        <el-input v-model="form.goods_model" placeholder="请输入产品型号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="产品分类" prop="cate_id">
                        <el-cascader v-model="form.cate_id" :options="cateList" :props="cateProps" filterable clearable style="width: 100%;"></el-cascader>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="产品单位" prop="unit_id">
                        <el-select v-model="form.unit_id" filterable style="width: 100%" placeholder="请选择产品单位">
                            <el-option v-for="item in unitList" :key="item.id" :label="item.unit_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="孔数" prop="hole">
                        <el-input v-model="form.hole" placeholder="请输入孔数" clearable type="number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="采购价" prop="purchase_price">
                        <el-input v-model="form.purchase_price" placeholder="请输入采购价" clearable type="number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="销售价" prop="sell_price">
                        <el-input v-model="form.sell_price" placeholder="请输入销售价" clearable type="number"></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item label="是否有颜色" prop="color_show">
                        <el-switch v-model="form.color_show" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="速比" prop="speed_ratio_id">
                        <el-select v-model="form.speed_ratio_id" filterable style="width: 100%" placeholder="请选择速比">
                            <el-option v-for="item in speedList" :key="item.id" :label="item.name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="法兰" prop="flange_id">
                        <el-select v-model="form.flange_id" filterable style="width: 100%" placeholder="请选择法兰">
                            <el-option v-for="item in flangeList" :key="item.id" :label="item.name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item label="颜色" prop="color_id">
                        <el-select v-model="form.color_id" filterable style="width: 100%" placeholder="请选择颜色">
                            <el-option v-for="item in colorList" :key="item.id" :label="item.name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row :gutter="1">
                <!-- <el-col :span="8">
                    <el-form-item label="仓库" prop="ware_id">
                        <el-select v-model="form.ware_id" filterable style="width: 100%" placeholder="请选择仓库">
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item label="高库存预警" prop="high_inventory_warning">
                        <el-input v-model="form.high_inventory_warning" placeholder="请输入高库存预警" clearable type="number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="低库存预警" prop="low_inventory_warning">
                        <el-input v-model="form.low_inventory_warning" placeholder="请输入低库存预警" clearable type="number"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="产品图片" prop="goods_image">
                <sc-upload-multiple height="100" width="100" v-model="form.goods_image" draggable></sc-upload-multiple>
            </el-form-item>
            <el-form-item label="附件" prop="enclosure">
                <sc-upload-file v-model="form.enclosure" :limit="1">
                    <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                </sc-upload-file>
            </el-form-item>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注1" prop="remark_one">
                    <el-input v-model="form.remark_one" placeholder="请输入备注1" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注2" prop="remark_two">
                    <el-input v-model="form.remark_two" placeholder="请输入备注2" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注3" prop="remark_three">
                    <el-input v-model="form.remark_three" placeholder="请输入备注3" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注4" prop="remark_four">
                    <el-input v-model="form.remark_four" placeholder="请输入备注4" clearable></el-input>
                </el-form-item>
            </el-col>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增产品',
				edit: '编辑产品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
				goods_model: "",
				cate_id: "",
				unit_id: "",
				hole: "",
				sell_price: "",
				purchase_price: "",
				speed_ratio_id: "",
				flange_id: "",
				color_id: "",
				high_inventory_warning: "",
				low_inventory_warning: "",
				goods_image: "",
				remark: "",
				remark_one: "",
				remark_two: "",
				remark_three: "",
				remark_four: "",
				system_disable: "",
                color_show: 1,
			},
			//验证规则
			rules: {
				goods_model: [{ required: true, message: '请输入产品名称' }],
			},
            speedList: [],
            flangeList: [],
            colorList: [],
            unitList:[],
            wareList:[],
            cateList:[],
            cateProps: {
				value: "id",
				label: "cate_name",
                emitPath: false,
				checkStrictly: true
			},
		}
	},
	mounted() {
        this.getCate()  //分类
        this.getUnit() // 单位
        this.getWare() // 仓库
        this.getSpeedRatio() // 速比
        this.getFlange() // 法兰
        this.getColor() // 颜色
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.goods.edit.post(this.form);
					} else {
						res = await this.$API.goods.goods.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        getPid(data){
            this.form.cate_id = data
        },
		//表单注入数据
		async setData(data) {
            // var res = await this.$API.goods.goods.detail.get({id:data.id});
            this.form.id = data.id
            this.form.goods_model = data.goods_model
            // let cate = data.cate_id.split(',')
            // let a = []
            // cate.map(item=>{
            //     a.push(Number(item))
            // })
            this.form.cate_id = data.cate_id
            this.form.unit_id = data.unit_id
            this.form.hole = data.hole
            this.form.sell_price = data.sell_price
            this.form.purchase_price = data.purchase_price
            this.form.speed_ratio_id = data.speed_ratio_id
            this.form.flange_id = data.flange_id
            this.form.color_id = data.color_id
            this.form.high_inventory_warning = data.high_inventory_warning
            this.form.low_inventory_warning = data.low_inventory_warning
            this.form.goods_image = data.goods_image
            this.form.remark = data.remark
            this.form.remark_one = data.remark_one
            this.form.remark_two = data.remark_two
            this.form.remark_three = data.remark_three
            this.form.remark_four = data.remark_four
            this.form.system_disable = data.system_disable
            this.form.color_show = data.color_show
		},
        // 获取分类
        async getCate(){
            var res = await this.$API.goods.goodsCate.all.get();
            this.cateList = res.data
        },
        // 获取仓库
        async getWare(){
            var res = await this.$API.goods.ware.all.get();
            this.wareList = res.data
        },
        // 获取单位
        async getUnit(){
            var res = await this.$API.goods.goodsUnit.all.get();
            this.unitList = res.data
        },
        // 获取速比
        async getSpeedRatio(){
            var res = await this.$API.common.getDictData.get({ type: "speedRatio" });
            let array = []
            res.data.speedRatio.map(item=>{
                array.push({
                    id:Number(item.id),
                    name:item.name
                })
            })
            this.speedList = array;
        },
        // 获取法兰
        async getFlange(){
            var res = await this.$API.common.getDictData.get({ type: "flange" });
            let array = []
            res.data.flange.map(item=>{
                array.push({
                    id:Number(item.id),
                    name:item.name
                })
            })
            this.flangeList = array;
        },
        // 获取速比
        async getColor(){
            var res = await this.$API.common.getDictData.get({ type: "color" });
            let array = []
            res.data.color.map(item=>{
                array.push({
                    id:Number(item.id),
                    name:item.name
                })
            })
            this.colorList = array;
        },
	}
}
</script>

<style></style>
